package ncsutest;
import com.sun.spot.testFramework.ITestCases;
import com.sun.spot.testFramework.ITestResultLog;

public class NCSUTestMonitor implements ITestResultLog {
	public static final int SUCCESS = 0;
	public static final int ERROR = 1;
	public static final int FAILURE = 2;

	private ITestResultLog sublog;
	private ITestCases tests;

	private int numTests;
	private int[] results;

	public NCSUTestMonitor(ITestResultLog sublog) {
		super();
		this.sublog = sublog;
	}
	
	public void setTestCases(ITestCases tests) {
		this.tests = tests;
		numTests = tests.testsToBeRun().length;
		results = new int[numTests];
	}

	public void startTestRun(String testClassName) {
		if (sublog != null) sublog.startTestRun(testClassName);
	}

	public void finishTestRun(int testCount, int failureCount, int errorCount, long l) {
		if (sublog != null) sublog.finishTestRun(testCount, failureCount, errorCount, l);
	}

	public void reportSuccess(int testNo) {
		int index = testNumberToIndex(testNo);
		results[index] = SUCCESS;

		if (sublog != null) sublog.reportSuccess(testNo);
	}

	public void reportError(int testNo) {
		int index = testNumberToIndex(testNo);
		results[index] = ERROR;

		if (sublog != null) sublog.reportError(testNo);
	}

	public void reportFailure(int testNumber, String exceptionMessage) {
		int index = testNumberToIndex(testNumber);
		results[index] = FAILURE;
		
		if (sublog != null) sublog.reportFailure(testNumber, exceptionMessage);
	}

	public void showResults() {
		if (sublog != null) sublog.showResults();
	}
	
	protected int testNumberToIndex(int number) {
		int[] testsToRun = tests.testsToBeRun();
		
		for (int i = 0; i < testsToRun.length; i++) {
			if (testsToRun[i] == number) return i;
		}
		return -1;
	}
	
	public int getNumTests() { return numTests; }
	
	public int getResultOfTest(int index) {
		return results[index];
	}
}
